package org.lyq.erp.auth.department.controller;

import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.auth.department.pojo.Department;
import org.lyq.erp.auth.department.service.DepartmentService;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.lyq.erp.utils.string.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/department" })
public class DepartmentController {
	private static Logger logger = LoggerFactory.getLogger(DepartmentController.class);

	@Resource
	private DepartmentService departmentService;

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		List<Department> list = this.departmentService.list();
		map.addAttribute("list", list);
		return "department_list";
	}

	@RequestMapping({ "/insertAndUpdate" })
	public String insertAndUpdate(Department department, Integer menuUuid, ModelMap map) {
		if (!insertValidation(department)) {
			department.popuperrorMessage = "true";
			map.put("dataBean", department);
			List<Department> list = this.departmentService.list();
			map.addAttribute("list", list);
			return "department_list";
		}
		try {
			if (department.getDepUuid() == null) {
				this.departmentService.insert(department);
			} else {
				this.departmentService.update(department);
			}
		} catch (Exception e) {

			logger.info(e.getMessage());
			department.addErrorMessage("depment004");
			department.popuperrorMessage = "true";
			map.put("dataBean", department);
			List<Department> list = this.departmentService.list();
			map.addAttribute("list", list);
			return "department_list";
		}
		return "redirect:/department/list?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/getDepartment" })
	@ResponseBody
	public JSONResult getDepartment(Integer depUuid) {
		if (depUuid != null) {
			Department department = this.departmentService.selectByPrimaryKey(depUuid);
			if (department != null) {
				return JSONResult.ok(department);
			}
			return JSONResult.errorMsg("depment003");
		}

		return JSONResult.errorMsg("depment003");
	}

	@RequestMapping({ "/delete" })
	public String delete(Department department, Integer menuUuid, ModelMap map) {
		if (department.getDepUuid() != null) {
			try {
				this.departmentService.delete(department.getDepUuid());
				return "redirect:/department/list?menuUuid=" + menuUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}
		department.addErrorMessage("depment005");
		map.put("dataBean", department);
		List<Department> list = this.departmentService.list();
		map.addAttribute("list", list);
		return "department_list";
	}

	private boolean insertValidation(Department department) {
		if (ObjectEmptyUtils.isEmptyString(department.getDepName())) {
			department.addErrorMessage("depName", "depment001");
		}
		if (!ValidationUtils.isPhone(department.getDepTele())) {
			department.addErrorMessage("depTele", "depment002");
		}
		return department.getMap().isEmpty();
	}
}
